package br.com.musclebook.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import br.com.musclebook.enums.ExerciseCategory;

@Entity
@Table(name = "ROUTINE_DAY_TRAINING")
public class RoutineDayTraining implements Serializable, Comparable<RoutineDayTraining> {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "ROUTINE_DAY_ID")
    private RoutineDay routineDay;

    @Enumerated(EnumType.ORDINAL)
    @Column(name = "EXERCISE_CATEGORY")
    private ExerciseCategory exerciseCategory;

    @OneToMany(mappedBy = "routineDayTraining", cascade = { CascadeType.ALL }, orphanRemoval = true)
    @OrderBy("orderNumber")
    private Set<ExerciseExecutionSet> exerciseExecutionSets;

    public RoutineDayTraining() {
	this.exerciseExecutionSets = new TreeSet<ExerciseExecutionSet>();
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public RoutineDay getRoutineDay() {
	return routineDay;
    }

    public void setRoutineDay(RoutineDay routineDay) {
	this.routineDay = routineDay;
    }

    public ExerciseCategory getExerciseCategory() {
	return exerciseCategory;
    }

    public void setExerciseCategory(ExerciseCategory exerciseCategory) {
	this.exerciseCategory = exerciseCategory;
    }

    public Set<ExerciseExecutionSet> getExerciseExecutionSets() {
	return this.exerciseExecutionSets;
    }

    public List<ExerciseExecutionSet> getExerciseExecutionSetsAsList() {
	return new ArrayList<ExerciseExecutionSet>(this.exerciseExecutionSets);
    }

    public void setExerciseExecutionSets(Set<ExerciseExecutionSet> exerciseExecutionSets) {
	this.exerciseExecutionSets = exerciseExecutionSets;
    }

    public boolean addExerciseExecutionSet(ExerciseExecutionSet exerciseExecutionSet) {
	return this.exerciseExecutionSets.add(exerciseExecutionSet);
    }

    public boolean removeExerciseExecutionSet(ExerciseExecutionSet exerciseExecutionSet) {
	return this.exerciseExecutionSets.remove(exerciseExecutionSet);
    }
    
    @Override
    public int compareTo(RoutineDayTraining o) {
	String thisRoutineName = this.getRoutineDay().getRoutine().getName();
	String thatRoutineName = o.getRoutineDay().getRoutine().getName();
	
	Integer thisRoutineDay = this.getRoutineDay().getDayOfWeek();
	Integer thatRoutineDay = o.getRoutineDay().getDayOfWeek();
	
	if(thisRoutineName.equals(thatRoutineName)) { // compare name
	    if(thisRoutineDay.equals(thatRoutineDay)) { // compare day
		return this.exerciseCategory.compareTo(o.getExerciseCategory());

	    } else {
		return thisRoutineDay.compareTo(thatRoutineDay);
	    }
	} else  {
	    return thisRoutineName.compareTo(thatRoutineName);
	}
    }

}
